package com.bds.btcdc.adapter.bitstamp;

import com.alibaba.fastjson.JSONObject;
import com.bds.btcdc.adapter.bitstamp.service.impl.BitstampServiceImpl;
import com.bds.btcdc.model.CoinPairExchanges;
import com.bds.btcdc.api.model.Result;
import com.bds.btcdc.util.Const;
import com.bds.btcdc.util.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.simp.stomp.StompSession;

import java.util.List;

/**
 * Created by DongChenchen on 2018/4/9 0009
 * 交易所：https://www.bitstamp.net
 */
public class BitstampTask extends Thread {
    private Logger logger = LoggerFactory.getLogger(getClass());

    private BitstampServiceImpl service = SpringContextHolder.getBean(BitstampServiceImpl.class);
    private StompSession session;
    private List<CoinPairExchanges> pairs;

    public BitstampTask(StompSession session,List<CoinPairExchanges> pairs) {
        this.session = session;
        this.pairs=pairs;
    }

    @Override
    public synchronized void run() {
        try {
            //        String ticker = service.getTicker("btcusd");
            Result marketInfo = service.getMarketInfo(pairs);
            if (marketInfo.getCode() == Const.SUC) {
                if (session != null) {
                    //推到btc-web主题
                    session.send(Const.BITSTAMP.MARKET_TOPIC, JSONObject.toJSONString(marketInfo).getBytes());
                }
            } else {
                logger.info(JSONObject.toJSONString(marketInfo));
            }
        }catch (Exception e){
            logger.info("Bitstamp 连接平台websocket服务,推送数据发生异常！"+e.getMessage());
        }
    }

}
